using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BLL;
using Model;

namespace UI {
    public partial class FrmMain : Form {
        private RoomBLL _roomBLL = RoomBLL.Instance;
        private DeskBLL _deskBLL = DeskBLL.Instance;
        public FrmMain() {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        //会员管理
        public static FrmMemberOperation ObjFrmMemberOperation;
        private void btnMemmber_Click(object sender, EventArgs e) {
            if (ObjFrmMemberOperation == null) {
                ObjFrmMemberOperation = new FrmMemberOperation();
                ObjFrmMemberOperation.Show();
            } else {
                ObjFrmMemberOperation.Activate();
                ObjFrmMemberOperation.WindowState = FormWindowState.Normal;
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) {
            Application.Exit();
        }

        //商品管理
        public static FrmProductOperation ObjFrmProductOperation;
        private void btnCategory_Click(object sender, EventArgs e) {
            if (ObjFrmProductOperation == null) {
                ObjFrmProductOperation = new FrmProductOperation();
                ObjFrmProductOperation.Show();
            } else {
                ObjFrmProductOperation.Activate();
            }
        }

        //加载包厢
        private void FrmMain_Load(object sender, EventArgs e) {
            DataTrans += _frmAddOrder.InitFrmAddOrder; 
            
            List<Room> rooms = _roomBLL.QueryAllRoom();

            foreach (Room room in rooms) {
                tabControl.TabPages.Add(new TabPage(room.RoomName) {
                    Tag = room
                });
            }

            UpdateDeskInRoom();
        }

        private void UpdateDeskInRoom() {
            //拿到当前表和表tag中的房间信息
            TabPage tabPage = tabControl.SelectedTab;
            Room room = tabPage.Tag as Room;
            //根据roomid拿到房间信息
            List<Desk> desks = _deskBLL.QueryByRoomId(room.RoomId);
            
            ListView listView = new ListView() {
                View = View.LargeIcon,
                MultiSelect = false,
                LargeImageList = imageList1,
                Dock = DockStyle.Fill
            };
            //将房间信息填充到listview中
            foreach (Desk desk in desks) {
                listView.Items.Add(new ListViewItem() {
                    Tag = desk,
                    Text = desk.DeskName,
                    ImageIndex = desk.DeskState
                });
            }
            
            tabPage.Controls.Add(listView);
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e) {
            UpdateDeskInRoom();
        }

        private event Action<RoomAndDesk> DataTrans;
        private FrmAddOrder _frmAddOrder = new FrmAddOrder();
        private void btnBiling_Click(object sender, EventArgs e) {
            //拿到当前选中的房间
            TabPage tabPage = tabControl.SelectedTab;
            ListView listView = tabPage.Controls[0] as ListView;
            //是否选中房间
            if (listView.SelectedItems.Count == 0) {
                MessageBox.Show("you havn't select a desk");
                return;
            }
            //看桌子是否被占用
            Desk desk = listView.SelectedItems[0].Tag as Desk;
            if (desk.DeskState == 1) {
                MessageBox.Show("The table is occupied");
                return;
            }
            //真开单
            if (_frmAddOrder == null) {
                _frmAddOrder = new FrmAddOrder();
                _frmAddOrder.Closing += (o, s) => {_frmAddOrder = null;};
            }
            DataTrans.Invoke(new RoomAndDesk() {
                Room = tabPage.Tag as Room,
                Desk = desk
            });
            DialogResult dialogResult = _frmAddOrder.ShowDialog();
            if (dialogResult == DialogResult.OK) {
                UpdateDeskInRoom();
            }
        }

        //增加消费窗口
        private event Action<RADwithOrderId> TransToFrmOrderWithProduct;
        private void btnMoney_Click(object sender, EventArgs e) {
            //拿到当前选中的房间
            TabPage tabPage = tabControl.SelectedTab;
            ListView listView = tabPage.Controls[0] as ListView;
            //是否选中房间
            if (listView.SelectedItems.Count == 0) {
                MessageBox.Show("you havn't select a desk");
                return;
            }
            //看桌子是否开单
            Desk desk = listView.SelectedItems[0].Tag as Desk;
            if (desk.DeskState == 0) {
                MessageBox.Show("The table is free");
                return;
            }
            FrmOrderWithProduct _frmOrderWithProduct = new FrmOrderWithProduct();
            TransToFrmOrderWithProduct += _frmOrderWithProduct.InitFrm;
            TransToFrmOrderWithProduct.Invoke(new RADwithOrderId() {
                OrderId = _deskBLL.QueryOrderIdByDeskId(desk),
                Desk = desk,
                Room = tabPage.Tag as Room
            });
            _frmOrderWithProduct.ShowDialog();
        }

        //结账
        private event Action<RADwithOrderId> TransToFrmCheckout; 
        private void button1_Click(object sender, EventArgs e) {
            //拿到当前选中的房间
            TabPage tabPage = tabControl.SelectedTab;
            ListView listView = tabPage.Controls[0] as ListView;
            //是否选中房间
            if (listView.SelectedItems.Count == 0) {
                MessageBox.Show("you havn't select a desk");
                return;
            }
            //看桌子是否开单
            Desk desk = listView.SelectedItems[0].Tag as Desk;
            if (desk.DeskState == 0) {
                MessageBox.Show("The table is free");
                return;
            }
            FrmCheckout _frmCheckout = new FrmCheckout();
            TransToFrmCheckout += _frmCheckout.Init;
            TransToFrmCheckout.Invoke(new RADwithOrderId() {
                OrderId = _deskBLL.QueryOrderIdByDeskId(desk),
                Desk = desk,
                Room = tabPage.Tag as Room
            });
            _frmCheckout.ShowDialog();
        }
    }
}